package baiqitun.stupro.api.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author kedaji
 * Spring MVC 配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 配置FastJson为默认的json序列化器
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //格式化日期时间
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteNullListAsEmpty
        );
        ParserConfig parserConfig = new ParserConfig();
        parserConfig.addDeny("stackTrace");
        parserConfig.addDenyInternal("stackTrace");
        fastJsonConfig.setParserConfig(parserConfig);
        converter.setFastJsonConfig(fastJsonConfig);
        //将FastJson转换器加入到Jackson转换器之前
        converters.add(0, converter);
    }
}
